﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Saker.Kernel.BLL;
using Saker.Kernel.DAL;
using System.Collections;
using Saker.Kernel.Model;

namespace Saker.Web.Boss.Sys.Modules
{
    public partial class ModulesList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }
        public void BindData()
        {
            RepModulesList.DataSource = new SysModuleBLL().GetAll();
            RepModulesList.DataBind();
        }

        protected void RepModulesList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "EditModules")
            {
                Response.Redirect("ModuleAdd.aspx?cmd=Edit&moduleID=" + e.CommandArgument.ToString());
            }
            if (e.CommandName == "AddChild")
            {
                Response.Redirect("ModuleAdd.aspx?cmd=Add&parentID=" + e.CommandArgument.ToString());
            }
            if (e.CommandName == "DeleteModules")
            {
                if (new SysModuleBLL().DeleteByModuleID(int.Parse(e.CommandArgument.ToString())) > 0)
                {
                    BindData();
                    ScriptManager.RegisterStartupScript((Page)this, this.Page.GetType(), "alert", "$(function () { top.jBox.tip('模块删除成功！', 'success'); });", true);
                }
            }
        }

        protected void RepModulesList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            CheckBoxList cbList = (CheckBoxList)e.Item.FindControl("cbAuthority");
            Label lab_ID = (Label)e.Item.FindControl("lab_ID");
            IEnumerable<SysAuthorityDir> list = new SysAuthorityDirBLL().GetAll();
            cbList.DataSource = list;
            cbList.DataTextField = "AuthorityName";
            cbList.DataValueField = "AuthorityTag";
            cbList.RepeatColumns = list.Count<SysAuthorityDir>();
            cbList.DataBind();
            DataTable dt = new SysModuleAuthorityListBLL().GetAllToTable();
            DataRow[] rows = dt.Select("ModuleID='"+lab_ID.Text+"'");
            foreach (DataRow row in rows)
            {
                cbList.Items.FindByValue(row["AuthorityTag"].ToString()).Selected = true;
            }
        }
    }
}